package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.utils.FileUploadUtil;
import com.yungu.swift.api.utils.RandomUtil;
import com.yungu.swift.assets.driver.model.dto.DriverFaceRecognitionDto;
import com.yungu.swift.assets.driver.service.DriverFaceRecognitionService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.common.model.dto.CommonArticleDto;
import com.yungu.swift.common.model.dto.CommonErrorMessageDto;
import com.yungu.swift.common.model.dto.CommonTagDto;
import com.yungu.swift.common.model.vo.AppVersionVo;
import com.yungu.swift.common.service.*;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Zerrion
 * @description
 * @date 2016/12/6. <br>
 */
@Controller
@RequestMapping("/api/v1/driver")
@Api(value = "【司机】【通用】司机通用接口", tags = "司机公用接口相关操作")
public class DriCommonApi extends BaseApi {
    @Reference
    private CommonArticleService commonArticleService;
    @Reference
    private CommonTagService commonTagService;
    @Reference
    private CommonVersionService versionService;
    @Reference
    private CommonErrorMessageService commonErrorMessageService;
    @Reference
    private DriverFaceRecognitionService driverFaceRecognitionService;
    @Reference
    private CommonSmsService commonSmsService;

    @ResponseBody
    @RequestMapping(value = {"/tag/problem"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取常见问题", response = ResponseData.class, notes = "list")
    public ResponseData<List<CommonArticleDto>> listOfProblem(@ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        Map<String, Object> map = MapUtils.build(4);
        map.put("articleType", CommonConstant.DRIVER);
        map.put("appid", appid);
        ResponseData<List<CommonArticleDto>> responseData = commonArticleService.list(map);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            List<CommonArticleDto> data = responseData.getData();
            for (CommonArticleDto articleDto : data) {
                articleDto.setArticleSimpleContent(null);
                articleDto.setArticleContent(null);
            }
        }
        return responseData;
    }

    @RequestMapping(value = {"/tag/complain"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取投诉列表", response = ResponseData.class, notes = "list")
    public ResponseData<List<CommonTagDto>> listOfComplain(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        Map<String, Object> map = MapUtils.build(3);
        map.put("tagType", CommonConstant.TAG_TYPE_COMPLAIN);
        map.put("tagStatus", MagicConstant.INT_ONE);
        map.put("identity", CommonConstant.DRIVER);
        map.put("appid", appid);
        return commonTagService.list(map);
    }


    @RequestMapping(value = {"/tag/cancelMsg"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取取消原因列表", response = ResponseData.class, notes = "list")
    public ResponseData<List<CommonTagDto>> listOfCancelMsg(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        Map<String, Object> map = MapUtils.build(3);
        map.put("tagType", CommonConstant.TAG_TYPE_CANCEL);
        map.put("tagStatus", MagicConstant.INT_ONE);
        map.put("identity", CommonConstant.DRIVER);
        map.put("appid", appid);
        return commonTagService.list(map);
    }


    @RequestMapping(value = {"/version/check"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "版本更新", response = ResponseData.class)
    public ResponseData<AppVersionVo> check(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "platform", value = "平台（android/ios）", required = true) @RequestParam String platform,
            @ApiParam(name = "versionNo", value = " 版本No", required = true) @RequestParam String versionNo) {
        if (StringUtils.isEmpty(platform) || StringUtils.isEmpty(versionNo)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数不能为空");
        }
        return versionService.checkVersion(platform, versionNo, CommonConstant.DRIVER, appid);
    }


    @RequestMapping(value = {"/identifyCode/send"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "发送验证码", response = ResponseData.class, notes = "发送短信验证码接口")
    public ResponseData<Boolean> send(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "mobile", value = "手机号码", required = true) @RequestParam String mobile,
            @ApiParam(name = "countryMobileCode", value = "国家手机号码前缀", required = false) @RequestParam(required = false) String countryMobileCode) {
        String decryptMobile = RandomUtil.randomDecrypt(mobile);
        if (!ValidateUtils.validateMobile(decryptMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        return commonSmsService.sendAliIdentify(decryptMobile, appid);
    }

    @RequestMapping(value = {"/token/uploadErrorMsg"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "上传错误日志", response = ResponseData.class, notes = "上传错误日志")
    public ResponseData<Boolean> uploadErrorMsg(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam("token") String token,
            @ApiParam(name = "errorTime", value = "错误时间(字符串类型，格式 2015-05-01 11:11:11)", required = true) @RequestParam String errorTime,
            @ApiParam(name = "errorModule", value = "错误模块(目前先定义长连接为：PUSH)", required = true) @RequestParam String errorModule,
            @ApiParam(name = "errorType", value = "错误类型(手机端的错误类型，比如安卓NullPointException，主要是方便查问题)", required = true) @RequestParam String errorType,
            @ApiParam(name = "errorMsg", value = "错误信息(可将整串错误文本传上来，或者前端自己处理过传上来)", required = true) @RequestParam String errorMsg,
            @ApiParam(name = "terminalId", value = "手机Id(手机唯一标识id)", required = false) @RequestParam(required = false) String terminalId,
            @ApiParam(name = "terminalVersion", value = "手机版本(例 iPhone 7 Plus -- 10.3.2)", required = false) @RequestParam(required = false) String terminalVersion,
            HttpServletRequest request) {
        CommonErrorMessageDto commonErrorMessageDto = new CommonErrorMessageDto();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            commonErrorMessageDto.setErrorTime(simpleDateFormat.parse(errorTime));
        } catch (Exception e) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                commonErrorMessageDto.setErrorTime(simpleDateFormat.parse(errorTime));
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        }
        commonErrorMessageDto.setUuid(StringUtils.buildUUID());
        commonErrorMessageDto.setCreateOn(new Date());
        commonErrorMessageDto.setCreateBy(getUserUid(request));
        commonErrorMessageDto.setUploadFrom(CommonErrorMessageDto.UPLOAD_FROM_DRIVER);
        commonErrorMessageDto.setErrorModule(errorModule);
        commonErrorMessageDto.setErrorType(errorType);
        commonErrorMessageDto.setErrorMsg(errorMsg);
        commonErrorMessageDto.setTerminalId(terminalId);
        commonErrorMessageDto.setTerminalVersion(terminalVersion);
        return commonErrorMessageService.add(commonErrorMessageDto);
    }

    @RequestMapping(value = {"/faceRecognition/verify"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机人脸识别", response = ResponseData.class, notes = "司机人脸识别")
    public ResponseData<Map<String, Object>> faceRecognition(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "mobile", value = "手机号码", required = true) @RequestParam String mobile,
            @ApiParam(name = "scene", value = "使用场景", required = true) @RequestParam String scene,
            HttpServletRequest request) {
        Map<String, Object> map = MapUtils.build();
        map.put("mobile", mobile);
        map.put("scene", scene);
        return driverFaceRecognitionService.checkCertification(map);
    }

    @RequestMapping(value = {"/faceRecognition/informResult"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机人脸识别", response = ResponseData.class, notes = "司机人脸识别")
    public ResponseData<Object> informStatus(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "mobile", value = "手机号码", required = true) @RequestParam String mobile,
            @ApiParam(name = "status", value = "认证状态", required = true) @RequestParam Integer status) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("mobile", mobile);
        map.put("status", status);
        ResponseData<DriverFaceRecognitionDto> responseData = driverFaceRecognitionService.informResult(map);
        if (responseData.isSuccess()) {
            DriverFaceRecognitionDto driverFaceRecognitionDto = responseData.getData();
            //当图片不为空的时候 保存图片的认证记录表 作为二次认证图片资料
            if (driverFaceRecognitionDto.getFacePic() != null) {
                driverFaceRecognitionDto = FileUploadUtil.uploadImageByUrl(appid, driverFaceRecognitionDto);
            }
            driverFaceRecognitionService.edit(driverFaceRecognitionDto);
        }
        return ResponseData.buildSuccessResponse("通知成功", null);
    }

}
